package com.monboo.display
{
	import com.monboo.utils.Action;
	import com.monboo.utils.DragRoll;
	import com.monboo.utils.RollPage;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	public class ThumbRoll extends ChangeUI
	{
		private var thumbArr:Array = new Array();
		private var thumbUi:Class;
		private var vars:Object;
		private var pageNum:uint;
		private var totalPage:Number;
		private var thumbWidth:uint;
		private var thumbHeight:uint;
		private var thumbMargin:uint;
		private var thumbPad:uint;
		
		private var picArr:Array = new Array();
		private var ldArr:Array = new Array();
		private var loadedArr:Array = new Array();
		
		
		private var thumbContenet:Sprite;
		private var thumbCurrent:Sprite;
		private var bar:Sprite;
		private var dragLine:Sprite;
		
		private var lcontex:LoaderContext;
		private var lastThumb:Sprite;
		
		private var autoCurrentPage:int;
		private var endPage:int;
		private var hasSet:Boolean = false;
		private var aix:String;
		private var rollPage:RollPage;
		private var dragRoll:DragRoll;
		
		public var current:int;
		public var currentPage:int;
		
		
		public function ThumbRoll(_thumbArr:Array,_dragUi:Class,_thumbUi:Class,$vars:Object,$aix:String="x")
		{
			thumbArr = _thumbArr;
			thumbUi = _thumbUi;
			vars = $vars;
			thumbMargin = $vars["childMargin"];
			thumbPad = $vars["childPad"];
			thumbWidth = $vars["childWidth"];
			thumbHeight = $vars["childHeight"];
			pageNum = $vars["pageNum"];
			totalPage = Math.ceil(thumbArr.length/pageNum)-1;
			aix = $aix;
			
			var dragUi:Sprite = new _dragUi();
			bar = dragUi.getChildByName("thumbBar") as Sprite;
			dragLine = dragUi.getChildByName("dragLine") as Sprite;
			initRollPage();
			initDragRoll();
			hasSet = true;
		}
		private function initRollPage():void{	
			thumbContenet = new Sprite();
			thumbContenet.name = "thumbContenet";
			this.addChild(thumbContenet);

			for(var i:uint=0; i<thumbArr.length; i++){
				var loadState:Boolean = false;
				loadedArr.push(loadState);
				var ld:Loader = new Loader();
				ldArr.push(ld);
				
				var theThumbMc:Sprite = new thumbUi();
				thumbContenet.addChild(theThumbMc);
				
				var thumbCurrent:Sprite = theThumbMc.getChildByName("thumbCurrent") as Sprite;
				thumbCurrent.visible = false;
				
				var imageMc:Sprite = new Sprite();
				theThumbMc.addChild(imageMc);
				imageMc.buttonMode = true;
				imageMc.addEventListener(MouseEvent.MOUSE_DOWN, showPicFunc);
				imageMc.mouseChildren = false;
				picArr.push(imageMc);	
				var imgContent:Sprite = new Sprite();
				imgContent.mouseEnabled = false;
				imgContent.mouseChildren = false;
				imgContent.name = "imgContent";
				imageMc.addChild(imgContent);	
				
				if(aix == "x"){
					theThumbMc.x = i*(thumbWidth+thumbMargin);
					
				}else{
					theThumbMc.y = i*(thumbHeight+thumbMargin);
				}				
			}
			
			var rollObj:Object = {pageNum:pageNum,
				totalNum:thumbArr.length,
				childWidth:thumbWidth,
				childHeight:thumbHeight,
				childMargin:thumbMargin};
			rollPage = new RollPage();
			rollPage.setRoll(thumbContenet,rollObj,aix);
			currentPage = 0;
			endPage = Math.ceil(thumbArr.length/pageNum)-1;
			
			loadPage(currentPage);
			updateThumb(0);
		}
		private function initDragRoll():void{	
			var dragObj:Object = {pageNum:pageNum,
				totalNum:thumbArr.length,
				childWidth:thumbWidth,
				childHeight:thumbHeight,
				childMargin:thumbMargin};
			dragRoll = new DragRoll(bar,thumbContenet,dragLine,dragObj,aix);
			
			dragRoll.y = 0;	
			dragRoll.x = 0;
				
			addChild(dragRoll);
			
			dragRoll.addEventListener("loadPage",toLoadPage);
			
			//如果图片数小于pageNum，没有缩略图拖杆
			if(thumbArr.length<=pageNum){
				dragRoll.visible = false;
			}	
		}	
	
		public function set dragX(n:Number):void{
			dragRoll.x = n;
		}
		public function set dragY(n:Number):void{
			dragRoll.y = n;
		}
		public function nextPage():void{			
			if(currentPage<Math.ceil(thumbArr.length/pageNum)-1){
				loadPage(currentPage+1);
				currentPage++;
				rollPage.toRoll(rollPage.page+1);	
				
				dragRoll.rollDragDot(currentPage);
			}else if(currentPage == Math.ceil(thumbArr.length/pageNum)-1){
				//trace("这是最后一页");
				dispatchEvent(new Event("isLastPage"));			
			}	
		}
		
		public function prePage():void{		
			if(currentPage>0){
				loadPage(currentPage-1);
				currentPage--;
				rollPage.toRoll(rollPage.page-1);
				
				dragRoll.rollDragDot(currentPage);
			}else if(currentPage == 0){
				//trace("这是第一页");
				dispatchEvent(new Event("isFirstPage"));
			}
		}
		public function firstPage():void{
			loadPage(0);
			currentPage = 0;
			rollPage.toRoll(1);
		}
		public function lastPage():void{
			loadPage(endPage);
			currentPage = endPage;
			rollPage.toRoll(lastPage+1);
		}
		public function hideDrag():void{
			dragRoll.visible = false;
		}
		public function pickPosition(n:Number):void{
			dragRoll.pickThumbBar(n);
		}
		public function updateThumb(n:Number):void{
			current = n;
			if(lastThumb){lastThumb.getChildByName("thumbCurrent").visible = false;}
			
			var theThumb:Sprite = thumbContenet.getChildAt(n) as Sprite;
			theThumb.getChildByName("thumbCurrent").visible = true;
			
			lastThumb = theThumb;
			if(hasSet){autoRollPage();}
		}
		public function autoRollPage():void{	
			if(current==Math.ceil(current/pageNum)*pageNum-1){
				autoCurrentPage = Math.ceil(current/pageNum)-1;
				loadPage(autoCurrentPage);
				currentPage = autoCurrentPage;
				
				rollPage.toRoll(currentPage+1);
				dragRoll.rollDragDot(currentPage);
			}else if(current==Math.ceil(current/pageNum)*pageNum){
				autoCurrentPage = Math.ceil(current/pageNum);
				loadPage(autoCurrentPage);
				currentPage = autoCurrentPage;
				
				rollPage.toRoll(currentPage+1);
				dragRoll.rollDragDot(currentPage);
			}		
		}
		
		
		private function toLoadPage(e:Event):void{
			var dragPage:Number = dragRoll.getDragPage()+1;
			loadPage(dragPage);
			
			currentPage = dragPage-1;
			rollPage.page = currentPage+1;
		}
		private function loadPage(n:Number):void{	
			if(n<0 || n>totalPage){
				return;
			}
			//这里求出每一个分页的最大图片数。如果图片总数小于设置的每页图片数，那k等于图片总数
			//如果图片总数大于设置的每页图片数，只在最后一页k等于图片总数除每页图片数的余,其它页k等于每页图片数
			var k:Number = lastPageNum(n);
			
			//启动此页中的图片加载器，求出各图片的url地址进行加载
			//图片序号：pageNum*n+i
			//图片url：thumbArr[pageNum*n+i]
			for(var i:uint=0; i<k; i++){
				var num:Number = pageNum*n+i;
				var tempurl:String = thumbArr[num];
				
				if(tempurl){
					if(!loadedArr[num]){
						var ld:Loader = ldArr[num];
						ld.load(new URLRequest(tempurl.substring(0,tempurl.length-4)+"_s.jpg"),lcontex);
						ld.contentLoaderInfo.addEventListener(Event.COMPLETE,loadedFun);
					}
				}else{
					if(!loadedArr[num]){
						var ldPic:Loader = ldArr[num];
						
						ldPic.load(new URLRequest(thumbArr[num]),lcontex);
						ldPic.contentLoaderInfo.addEventListener(Event.COMPLETE,loadedFun);
						trace("[***此图未找到缩略图，已加载大图为缩略图]");
					}
				}
				
			}		
		}
		private function showPicFunc(e:MouseEvent):void{
			var n:Number = picArr.indexOf(e.target);
			if(current != n){
				current = n;
				updateThumb(current);
				dispatchEvent(new Event("gotoImage"));
			}
		}
		private function lastPageNum(n:Number):Number{
			var k:Number;
			if(thumbArr.length>pageNum){
				if(n == Math.ceil(thumbArr.length/pageNum)-1){
					k = thumbArr.length%pageNum;
					if(k ==0){
						k = pageNum;
					}	
				}else{
					k= pageNum;
				}
			}else{
				k =thumbArr.length;	
			}
			return k;
		}
		private function loadedFun(e:Event):void{
			var ld:Loader = e.target.loader as Loader;
			var num:Number = ldArr.indexOf(ld);
			
			var pic:Sprite = picArr[num];
			loadedArr[num] = true;
			
			if(pic){
				var imgContent:Sprite = pic.getChildByName("imgContent") as Sprite;
				imgContent.addChild(ld);	
				changeSize(imgContent);
			}
		}	
		
		override public function changeSize(pic:Sprite):void {	
			w = pic.width;
			h = pic.height;
			
			setSize(pic,thumbWidth-thumbPad*2,thumbHeight-thumbPad*2,w,h);
			setPosition(pic,thumbWidth,thumbHeight);			
		}
		override protected function setSize(pic:Sprite,bigPicWidth:Number,bigPicHeight:Number,w:Number,h:Number):void{
			pic.parent.scaleX = pic.parent.scaleY = 1;
			if(w/h > bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicWidth/w;
			}else if(w/h < bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicHeight/h;
			}else if(w/h == bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicWidth/w;
			}
		}	
		override protected function setPosition(pic:Sprite,w:Number,h:Number):void{				
			pic.parent.x += (w - pic.parent.width)/2;
			pic.parent.y += (h - pic.parent.height)/2;
		}
	}
}